<template>
  <div class="roles-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table
        :data="rolesList"
        border
        style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row
              class="border-bot standard-center" 
              :class="[index === 0 ? 'border-top' : '']"
              v-for="(item, index) in props.row.children" 
              :key="item.id">
              <el-col :span="5">
                <el-tag type="primary" @close="deleteAuthName(item.id)" closable>{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row 
                  class="standard-center"
                  :class="[index2 === 0 ? '' : 'border-top']"
                  v-for="(item2, index2) in item.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag @close="deleteAuthName(item2.id)" type="success" closable>{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning" 
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      @close="deleteAuthName(item3.id)"
                      closable>
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column
          type="index"
          label="#"
          width="50">
        </el-table-column>
       <el-table-column
          prop="roleName"
          label="角色名称">
        </el-table-column>
        <el-table-column
          prop="roleDesc"
          label="角色描述">
        </el-table-column>
        <el-table-column
          label="操作"
          width="300">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showEditRights(scope.row)">设置</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="修改权限"
      @close="closeDialog"
      :visible.sync="rightsDialogVisible"
      width="50%">
      <el-tree
        :data="rightsList"
        show-checkbox
        ref="tree"
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="checkedKeysList"
        :props="treeDefaultProps">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitRightsList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script> 
export default {
  data() {
    return {
      rolesList: [],
      rightsList: [],
      checkedKeysList: [],
      rightsDialogVisible: false,
      treeDefaultProps: {
        label: 'authName',
        children: 'children'
      },
      roleId: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      const {data: res} = await this.$http.get('roles')

      if(res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.rolesList = res.data
    },
    deleteAuthName() {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })        
      })
    },
    async showEditRights(row) {
      const {data: res} = await this.$http.get('rights/tree')

      if(res.meta.status !== 200) {
        return this.$message.error(res.meta.status)
      }
      this.rightsList = res.data

      this.handelCheckedKeys(row, this.checkedKeysList)

      this.roleId = row.id
      this.rightsDialogVisible = true
    },
    handelCheckedKeys(node, arr) {
      if( !node.children ) {
        return arr.push(node.id)
      }

      node.children.forEach(item => {
        this.handelCheckedKeys(item, arr)
      })
    },
    closeDialog() {
      this.checkedKeysList = []
    },
    async submitRightsList() {
      const arr = [
        ...this.$refs.tree.getHalfCheckedKeys(),
        ...this.$refs.tree.getCheckedKeys()
      ]

      const {data: res} = await this.$http.post(`roles/${this.roleId}/rights`, {rids: arr.toString()})

      if(res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.getRolesList()

      this.rightsDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.roles-container {
  .box-card{
    margin-top: 20px;
  }
  .el-table{
    margin-top: 20px;
  }
  .el-tag{
    user-select: none;
    margin: 8px;
  }
  .border-bot{
    border-bottom: 1px solid #EBEEF5;
  }
  .border-top{
    border-top: 1px solid #EBEEF5;
  }
  .standard-center {
    display: flex;
    align-items: center;
  }
}
</style>
